home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Extensions / Imaging / PIL / McIdasImagePlugin.py < prev    next >
Encoding:
Text File  |  2000-06-23  |  1.7 KB  |  72 lines

  1. #
  2. # The Python Imaging Library.
  3. # $Id: McIdasImagePlugin.py,v 1.1.1.1 1998/08/18 13:07:57 sjoerd Exp $
  4. #
  5. # Basic McIdas support for PIL
  6. #
  7. # History:
  8. #       97-05-05 fl     Created (8-bit images only)
  9. #
  10. # Thanks to Richard Jones <richard.jones@bom.gov.au> for specs
  11. # and samples.
  12. #
  13. # Copyright (c) Secret Labs AB 1997.
  14. # Copyright (c) Fredrik Lundh 1997.
  15. #
  16. # See the README file for information on usage and redistribution.
  17. #
  18.  
  19. __version__ = "0.1"
  20.  
  21. import string
  22.  
  23. import Image, ImageFile
  24.  
  25. def i16(c,i=0):
  26.     return ord(c[1+i])+(ord(c[i])<<8)
  27.  
  28. def i32(c,i=0):
  29.     return ord(c[3+i])+(ord(c[2+i])<<8)+(ord(c[1+i])<<16)+(ord(c[i])<<24)
  30.  
  31. def _accept(s):
  32.     return i32(s) == 0 and i32(s, 4) == 4
  33.  
  34. class McIdasImageFile(ImageFile.ImageFile):
  35.  
  36.     format = "MCIDAS"
  37.     format_description = "McIdas area file"
  38.  
  39.     def _open(self):
  40.  
  41.         # parse area file directory
  42.         s = self.fp.read(256)
  43.         if not _accept(s):
  44.             raise SyntaxError, "not an McIdas area file"
  45.  
  46.         # get mode
  47.         if i32(s, 40) != 1 or i32(s, 52) != 1:
  48.             raise SyntaxError, "unsupported McIdas format"
  49.  
  50.         self.mode = "L"
  51.  
  52.         # get size
  53.         self.size = i32(s, 36), i32(s, 32)
  54.  
  55.         # setup image descriptor
  56.         prefix = i32(s, 56)
  57.         offset = i32(s, 132)
  58.  
  59.         self.tile = [("raw", (0, 0) + self.size, offset,
  60.                      ("L", prefix + self.size[0], 1))]
  61.  
  62.         # FIXME: should store the navigation and calibration blocks
  63.         # somewhere (or perhaps extract some basic information from
  64.         # them...)
  65.  
  66. # --------------------------------------------------------------------
  67. # registry
  68.  
  69. Image.register_open("MCIDAS", McIdasImageFile, _accept)
  70.  
  71. # no default extension
  72.